<?php
class LoggedUserController extends RestrictedBaseController {
	
	public function getIndex() {
		$this->layout->content = View::make('loggeduser.index');
	}
	
	public function postUpdatePassword() {
		$validator = Validator::make(Input::all(), User::$updatePasswordRules);
		
		if ($validator->fails()) {
			return Redirect::to('loggedUser')
				->withErrors($validator);
		} else {
			$credentials = array(
				'username' => Auth::user()->username,
				'password' => Input::get('currentPassword')
			);
			
			if (Auth::attempt($credentials, false, false)) {
				$user = Auth::user();
				$user->password = Hash::make(Input::get('password'));
				$user->save();
				
				Session::flash('successMessage', 'Senha atualizada com sucesso.');
			} else {
				Session::flash('errorMessage', 'A senha atual não confere.');
			}

			return Redirect::to('loggedUser');
		}
	}
	
}